package com.example.esaclient;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.util.Log;

import com.google.gson.Gson;


public class GetMyPosts extends AsyncTask<Object, Integer, String[]> {
	
	private String URL ;
	private  static final String METHOD_NAME = "/GetMyPosts?";	
	TabUserTimeline caller;
	
	public GetMyPosts(TabUserTimeline callerActivity){
		caller=callerActivity;
	}
	
	@Override
	protected String[] doInBackground(Object... params) {	
		Log.w("ESA","Calling GetMyPost...");
			try{
				 
				 GlobalSettings gs = new GlobalSettings(caller.getActivity());
				 URL = "http://"+gs.getPrefString(GlobalSettings.PREF_ESA_URL)+"/ServiceRest.svc";			 
				 String url =URL+METHOD_NAME+"id="+params[0]+"&where="+params[1]+"&start_offset="+params[2]+"&end_offset="+params[3];
						  
				 HttpClient client = new DefaultHttpClient();
			        HttpGet httpGet = new HttpGet(url);
			        try {
			            HttpResponse response = client.execute(httpGet);
			            HttpEntity entity = response.getEntity();
			             
			            if(entity != null) {
			                InputStream stream = entity.getContent();
			                String responseString = GlobalSettings.convertStreamToString(stream);		                
			                
			                Wrapper w = new Gson().fromJson(responseString, Wrapper.class);
			                   return w.GetMyPostsResult;
			                   
			            }
			
			  } catch (ClientProtocolException e) {
		            e.printStackTrace();	            
		        } 
			        catch (IOException e) {
		            e.printStackTrace();
		           
		        }
		         
			}
		     catch (final Exception e) {
		         e.printStackTrace();   
		       
		     }
			return new String[0]; 
			}
		
		
		   public class Wrapper {
		    	 public String[] GetMyPostsResult;
		     }
	
	

	     protected void onPostExecute( String[] result) {
	    	 Log.w("ESA","Exiting GetMyPost...");
	    	 if(caller!=null)
	    	 caller.GetMyPostsResult(result);
	     }

	
	
}